package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.mylhyl.superdialog.SuperDialog
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.ChooseGetCashPop
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.GainBean
import hlhj.fhp.supreme.javabean.UserInfoBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_self_gain_aty.*

class SelfGainAty : BaseAty() {
    private var bid=""
    private var getCashType=1;
    override fun getContentId(): Int = R.layout.activity_self_gain_aty
    private lateinit var chooseGetCashPop:ChooseGetCashPop
    private val CHOOSEBANK=1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun initView() {
        OkGo.post<UserInfoBean>(Urls.BaseUrl+Urls.UserInfo)
                .execute(object :JsonCallBack<UserInfoBean>(this){
                    override fun onSuccess(response: Response<UserInfoBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            tv14.text="我的余额：￥${bean?.data?.balance}"
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
        OkGo.post<GainBean>(Urls.BaseUrl+Urls.SELF_GAIN)
                .execute(object :JsonCallBack<GainBean>(this){
                    override fun onSuccess(response: Response<GainBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            tv1.text="自荐课收益：￥${bean?.data.class_income}"
                            tv2.text="推广收益：￥${bean?.data.mark_income}"
                            tv13.text="其他收益：￥${bean?.data.reward_income}"
                            totalGain.text="￥${bean?.data.class_income.toDouble()+bean?.data.mark_income.toDouble()+bean?.data.reward_income.toDouble()}"
                        }
                    }
                })

        chooseGetCashPop= ChooseGetCashPop(this,object :ChooseGetCashPop.OnChooseGetCashListener{
            override fun onAli() {
                getCashType=2
                loBank.visibility= View.GONE
                loAli.visibility=View.VISIBLE
            }

            override fun onBank() {
                getCashType=1
                loBank.visibility= View.VISIBLE
                loAli.visibility=View.GONE
            }
        })
        chooseGetCashPop.setOnDismissListener {
            val lp = window.attributes
            lp.alpha = 1f
            window.attributes = lp
        }
    }
    override fun initListener() {

        btHis.setOnClickListener{
            val intent = Intent(this, GetCashHisAty::class.java)
            startActivity(intent)
        }
        btExit.setOnClickListener {
            finish()
        }
        loBank.setOnClickListener {
            startActivityForResult(Intent(this@SelfGainAty,BankAty::class.java),CHOOSEBANK)
        }

        btGetCash.setOnClickListener {
            if (getCashType==1){
                if (bid.equals("")){
                    MyUtils.toast("请选择银行")
                }else {
                    if (etMoney.text.isEmpty()){
                        MyUtils.toast("请输入提现金额")
                    }else{
                        SuperDialog.Builder(this).setTitle("注意")
                                .setMessage("确认提现${etMoney.text}元到 ${defaultBank.text}账户？")
                                .setPositiveButton("确定"){
                                    OkGo.get<BaseBean>(Urls.BaseUrl+Urls.GETCASH)
                                            .params("bid",bid)
                                            .params("type",1)
                                            .params("money",etMoney.text.toString())
                                            .execute(object :JsonCallBack<BaseBean>(this){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val body = response?.body()
                                                    if (body?.code==1){
                                                        MyUtils.toast("提现申请成功")
                                                        finish()
                                                    }else{
                                                        MyUtils.toast(body?.msg)
                                                    }
                                                }
                                            })
                                }
                                .setNegativeButton("取消"){

                                }
                                .build()
                    }
                }
            }else{
                if (etAli.text.isEmpty()){
                    MyUtils.toast("请输入淘宝账号")
                }else{
                    if (etMoney.text.isEmpty()){
                        MyUtils.toast("请输入提现金额")
                    }else{
                        SuperDialog.Builder(this).setTitle("注意")
                                .setMessage("确认提现${etMoney.text}元到 ${etAli.text}账户？")
                                .setPositiveButton("确定"){
                                    OkGo.get<BaseBean>(Urls.BaseUrl+Urls.GETCASH)
                                            .params("type",2)
                                            .params("alipay",etAli.text.toString())
                                            .params("money",etMoney.text.toString())
                                            .execute(object :JsonCallBack<BaseBean>(this){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val body = response?.body()
                                                    if (body?.code==1){
                                                        MyUtils.toast("提现申请成功")
                                                        finish()
                                                    }else{
                                                        MyUtils.toast(body?.msg)
                                                    }
                                                }
                                            })
                                }
                                .setNegativeButton("取消"){

                                }
                                .build()
                    }
                }
            }


        }
        btMore.setOnClickListener {
            if (chooseGetCashPop.isShowing){
                chooseGetCashPop.dismiss()
            }else{
                chooseGetCashPop.showAtLocation(father,Gravity.BOTTOM,0,0)
                val lp = window.attributes
                lp.alpha = 0.5f
                window.attributes = lp
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        if (requestCode==CHOOSEBANK){
            defaultBank.text=data?.getStringExtra("name")
            bid=data?.getStringExtra("id")!!
        }
    }
}
